package com.ming.attendance.controller;

import com.ming.attendance.model.AttendanceEventCreateModel;
import com.ming.attendance.po.AttendanceEvent;
import com.ming.attendance.po.ResponseBean;
import com.ming.attendance.service.AttendanceEventService;
import com.ming.attendance.service.BeaconService;
import com.ming.attendance.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/AttendanceEvent")
public class AttendanceEventController {
    @Autowired
    private AttendanceEventService aes;
    @Autowired
    private BeaconService bs;

    @PostMapping("/createEvent")
    public ResponseBean<String> createEvent(@RequestHeader("Authorization") String token,@RequestBody AttendanceEventCreateModel event){
        String openid = JWTUtils.getOpenid(token);
        event.setOpenid(openid);
        System.out.println("event:>>>>>>>"+event);
        // 创建事件
        ResponseBean<String> result = aes.createEvent(event);

        return result;
    }

    @PostMapping("/updateEvent")
    public ResponseBean<Integer> updateEvent(AttendanceEvent event){
        System.out.println("event:>>>>>> "+event);
        return aes.updateEvent(event);
    }

    @PostMapping("/initEvent")
    public ResponseBean<Integer> initEvent(AttendanceEvent event){
        // System.out.println("event:>>>>>> "+event);
        return aes.updateEvent(event);
    }

    @GetMapping("/getEvent")
    public ResponseBean<AttendanceEvent> getEvent(String eventid){
        System.out.println("eventid:>>>>> "+eventid);
        return aes.getEvent(eventid);
    }

    @GetMapping("/getEventsByOpenid")
    public ResponseBean<List<AttendanceEvent>> getEventsByOpenid(@RequestHeader("Authorization") String token){
        String openid = JWTUtils.getOpenid(token);
        return aes.getEventsByOpenid(openid);
    }

    @PostMapping("/deleteEvent")
    public ResponseBean<Integer> deleteEvent(AttendanceEvent event){
        System.out.println(event);
        return aes.deleteEvent(event);
    }
}
